#include "stdafx.h"
#include "Serializer\IStorageSerializer.h"
#include <crtdbg.h>

using namespace persist;

IStorageSerializer::IStorageSerializer()
{
	mode = READ;
//	stream = 0;
//	storage = 0;
}
IStorageSerializer::IStorageSerializer(const std::string& _name)
{
	name = _name;
	mode = READ;
//	stream = 0;
//	storage = 0;

}
IStorageSerializer::IStorageSerializer(const std::string& _name, Persistor::Mode m)
{
	name = _name;
	mode = m;
//	stream = 0;
//	storage = 0;
}

/*
IStorageSerializer::IStorageSerializer(IStorage* _storage, IStream* _stream,
				   const std::string& _name, Persistor::Mode m) : mode(m)
{
	storage=_storage;
	stream=_stream;
	name=_name;
}
*/


IStorageSerializer::~IStorageSerializer()
{
	close();
}

bool IStorageSerializer::open(const std::string& fname, Persistor::Mode m)
{
//open storage and whatever streams you've got about here
	_ASSERTE(false);
	return false;
}
void IStorageSerializer::close()
{
//flush data
//close streams
//close storage
//clear data structures in memory
//
}

void IStorageSerializer::clear()
{
//Not implemented!
}

bool IStorageSerializer::isOpen()
{
//Not implemented!
	_ASSERTE(false);
	return false;
}

void IStorageSerializer::serialize(void* data, size_t size)
{
//read or write to active stream
//Not implemented!
	_ASSERTE(false);
}

//open a stream within structured storage  
/*
void IStorageSerializer::openStream(Persistor::Mode m, const std::string& name,IStream** pstream)
{

}
*/
void IStorageSerializer::fill(Persistent* _persistent)
{
//Not implemented!
	_ASSERTE(false);

	Persistent* persistent=_persistent;
//#ifdef _DEBUG
	Serializable* serializable=dynamic_cast<Serializable*>(persistent);
	_ASSERTE(serializable);
/*
#else
	Serializable* serializable=static_cast<Serializable*>(persistent);
#endif
*/

/*
	bool found=false;
	ulong internalAddress=0;
//find class id
	Table::const_iterator it = table.find(serializable->getIdentifier());
    if (iter != table.end())
    {
        Item::const_iterator iterI = iter->second.find(persistent->getName());
        if (iterI != iter->second.end())
        {
            internalAddress = iterI->second;
            found = true;
        }
    }

	if (!found)
    {
		if (mode == READ)
		{
			//can't read non-existent
			_ASSERTE(false);
		}
        internalAddress = table[serializable->getIdentifier()][persistent->getName()] = address;
        address++;
	}
//writing a new object. create new stream by internalAddress about here

*/
	serializable->serialize(*this);
}


//uncomment to override StandardSerializer::persist
/*
void IStorageSerializer::persist(Persistent*& _persistent)
{

}
*/
void IStorageSerializer::allocate(Persistent* _persistent)
{
//Not implemented!
	_ASSERTE(false);
}

std::string IStorageSerializer::allocate(const Identifier& id)
{
//Not implemented!
	_ASSERTE(false);
	const std::string emp("");
	return emp;
}
void IStorageSerializer::deallocate(Persistent* _persistent)
{
//Not implemented!
	_ASSERTE(false);
}

void IStorageSerializer::deallocate(const Identifier& id)
{
//Not implemented!
	_ASSERTE(false);
}
